#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAX_NAME_LENGTH 256
#define FILE_NAME "student.dat"
typedef struct student_info_t
{
	char name[MAX_NAME_LENGTH];
	int number;
	float math;
	float english;
	float soft;
}student_info;
typedef struct list_t
{
	student_info student;
	struct list_t *next;
}list;
/*************************************************
append a new student to the list ,if the list is NULL, a new header will be retured
head: point to the list
**************************************************/
list* list_append_new_student( list* head,student_info student)
{
	list* p_node;
	list* new_stu;
	list* p_tail;
	if(head == NULL ){
		head = (list*)malloc(sizeof(list));
		head->student = student;
		head->next =NULL;
		return head;
	}
	p_node = head;
	
	while(p_node != NULL){
		p_tail = p_node;
		p_node = p_node->next;
	}
	new_stu = (list*)malloc(sizeof(list));
	new_stu->student = student;
	new_stu->next = NULL;
	p_tail ->next = new_stu;
	return head;
}
/*****************************************************************
 write all the student's infomation in the list to the file
return 0 if success, -1 fail
****************************************************************/
int list_write_to_file(list* head,const char* file_name)
{
	FILE* fp;
	fp = fopen(file_name,"wb");
	if(fp == NULL) return -1;
	while(head!=NULL){
		if(fwrite(&head->student,sizeof(student_info),1,fp) != 1)
			return -1;
		head = head->next;		
	}
	fclose(fp);
	return 0;
}
/****************************************************************
 ***************************************************************/
list* list_read_from_file(const char* file_name)
{
	FILE* fp;
	long position = 0;
	unsigned long student_index;
	list* head = NULL;
	student_info stu;
	fp = fopen(file_name,"r");
	if(fp == NULL) return NULL;
	fseek(fp,0,SEEK_END);
	position = ftell(fp);
	fseek(fp,0,SEEK_SET);
	for(student_index = 0;student_index < position/sizeof(student_info);student_index++){
		fread(&stu,sizeof(student_info),1,fp);
		head = list_append_new_student(head,stu);
	}	
	return head;
}
int list_sort( list * head)
{
	return 0;
}
int list_print(list* head)
{
	//student_info *pHead;
	printf("Number\tName\tEnglish\tMath\tSoft\n");
	while(head != NULL){
		printf("%d\t%s\t%f\t%f\t%f\n",head->student.number,
			head->student.name,head->student.english,head->student.math,
			head->student.soft);
		head = head->next;
	}	
	return 0;
}
int file_append_new_student(const char* file_name,student_info stu)
{
	FILE* fp;
	fp = fopen(file_name,"ab");
	if(fwrite(&stu,sizeof(student_info),1,fp) != 1){
		fclose(fp);
		return -1;
	}
	fclose(fp);
	return 0;
}

int user_input_a_student(student_info* stu)
{
	return 0;
	
}

int main()
{
	student_info stu;
	list* head = NULL;
	list* ahead = NULL;
	
	strcpy(stu.name,"Zhu");
	stu.math = 99;
	stu.english = 93;
	stu.soft = 90;
	stu.number = 1;
	
		
	head = list_append_new_student(head,stu);
	head = list_append_new_student(head,stu);
	head = list_append_new_student(head,stu);
	head = list_append_new_student(head,stu);
	head = list_append_new_student(head,stu);

	list_write_to_file(head,FILE_NAME);
	list_print(head);
	ahead = list_read_from_file(FILE_NAME);
	list_print(ahead);
	
	
	
  
}
